package fr.dauphine.td3.bdd;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import fr.dauphine.td3.emp.Employe;


@Repository
@Transactional
public class EmployeDAO {
	
	
	private SessionFactory sessionFactory;
	
	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Transactional(readOnly = true)
	public List<Employe> get() {
		return (List<Employe>) getSession().createQuery("from Employe").list();
		/*
		 * Criteria
		 * return (List<Employe>) getSession().createCriteria(Employe.class).list();
		 * 
		 */
	}
	
	@Transactional(readOnly = true)
	public Employe get(String id) {
		Employe employe = (Employe) getSession().load(Employe.class, id);
		return employe;
	}
	
	@Transactional(readOnly=false, propagation = Propagation.REQUIRED)
	public void save(Employe employe) {
		employe.setValide(1);
		getSession().saveOrUpdate(employe);
	}

	@Transactional(readOnly=false, propagation = Propagation.REQUIRED)
	public void delete(Employe e) {
		getSession().delete(e);
	}
	
}
